<?php
session_start();

// 简单的密码保护
$admin_password = 'admin123';

if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: admin.php');
    exit;
}

if (!isset($_SESSION['admin_logged_in'])) {
    if (isset($_POST['password'])) {
        if ($_POST['password'] === $admin_password) {
            $_SESSION['admin_logged_in'] = true;
            header('Location: admin.php');
            exit;
        } else {
            $login_error = '密码错误';
        }
    }
    
    // 显示登录表单
    ?>
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>管理员登录</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
            }
            .login-container {
                background: white;
                padding: 40px;
                border-radius: 10px;
                box-shadow: 0 10px 30px rgba(0,0,0,0.3);
                text-align: center;
                max-width: 400px;
                width: 100%;
            }
            .login-container h1 {
                color: #333;
                margin-bottom: 30px;
            }
            .form-group {
                margin-bottom: 20px;
                text-align: left;
            }
            .form-group label {
                display: block;
                margin-bottom: 5px;
                color: #555;
                font-weight: bold;
            }
            .form-group input {
                width: 100%;
                padding: 12px;
                border: 1px solid #ddd;
                border-radius: 5px;
                font-size: 16px;
                box-sizing: border-box;
            }
            .login-btn {
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                color: white;
                padding: 12px 30px;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                font-size: 16px;
                width: 100%;
                margin-top: 10px;
            }
            .login-btn:hover {
                opacity: 0.9;
            }
            .error {
                color: #dc3545;
                margin-top: 10px;
                padding: 10px;
                background: #f8d7da;
                border: 1px solid #f5c6cb;
                border-radius: 5px;
            }
        </style>
    </head>
    <body>
        <div class="login-container">
            <h1>🔐 管理员登录</h1>
            <form method="post">
                <div class="form-group">
                    <label for="password">管理员密码:</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" class="login-btn">登录</button>
                <?php if (isset($login_error)): ?>
                    <div class="error"><?php echo htmlspecialchars($login_error); ?></div>
                <?php endif; ?>
            </form>
        </div>
    </body>
    </html>
    <?php
    exit;
}

// 引入数据库类
require_once 'database.php';

// 处理管理操作
$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    try {
        $db = new Database();
        
        switch ($_POST['action']) {
            case 'delete_visitor':
                if (isset($_POST['visitor_id'])) {
                    $visitor = $db->getVisitorById($_POST['visitor_id']);
                    if ($visitor && !empty($visitor['photo_filename'])) {
                        // 删除照片文件
                        $photoPath = 'visitor_data/photos/' . $visitor['photo_filename'];
                        if (file_exists($photoPath)) {
                            unlink($photoPath);
                        }
                    }
                    $db->deleteVisitor($_POST['visitor_id']);
                    $message = '访客数据删除成功';
                    $messageType = 'success';
                }
                break;
                
            case 'batch_delete':
                if (isset($_POST['selected_visitors']) && is_array($_POST['selected_visitors'])) {
                    $count = 0;
                    foreach ($_POST['selected_visitors'] as $visitorId) {
                        $visitor = $db->getVisitorById($visitorId);
                        if ($visitor && !empty($visitor['photo_filename'])) {
                            $photoPath = 'visitor_data/photos/' . $visitor['photo_filename'];
                            if (file_exists($photoPath)) {
                                unlink($photoPath);
                            }
                        }
                        $db->deleteVisitor($visitorId);
                        $count++;
                    }
                    $message = "成功删除了 {$count} 条访客数据";
                    $messageType = 'success';
                } else {
                    $message = '请选择要删除的数据';
                    $messageType = 'error';
                }
                break;
                
            case 'cleanup_old':
                $days = intval($_POST['cleanup_days'] ?? 30);
                $count = $db->cleanupOldData($days);
                $message = "成功清理了 {$count} 条超过 {$days} 天的旧数据";
                $messageType = 'success';
                break;
                
            case 'export_data':
                $format = $_POST['export_format'] ?? 'csv';
                exportData($db, $format);
                exit;
                
            case 'update_redirect_url':
                $redirectUrl = trim($_POST['redirect_url'] ?? '');
                $redirectDelay = intval($_POST['redirect_delay'] ?? 3);
                $enableRedirect = isset($_POST['enable_redirect']) ? 1 : 0;
                
                // 保存到配置文件
                $config = [
                    'redirect_url' => $redirectUrl,
                    'redirect_delay' => $redirectDelay,
                    'enable_redirect' => $enableRedirect
                ];
                
                if (file_put_contents('redirect_config.json', json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
                    $message = '跳转链接配置更新成功';
                    $messageType = 'success';
                } else {
                    $message = '配置文件保存失败';
                    $messageType = 'error';
                }
                break;
        }
    } catch (Exception $e) {
        $message = '操作失败: ' . $e->getMessage();
        $messageType = 'error';
    }
}

// 数据导出函数
function exportData($db, $format) {
    $visitors = $db->getAllVisitors();
    $filename = 'visitor_data_' . date('Y-m-d_H-i-s');
    
    if ($format === 'csv') {
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
        
        $output = fopen('php://output', 'w');
        fputcsv($output, ['ID', '时间', 'IP地址', '纬度', '经度', '精度', '照片', '用户代理', '来源']);
        
        foreach ($visitors as $visitor) {
            fputcsv($output, [
                $visitor['id'],
                $visitor['timestamp'],
                $visitor['ip_address'],
                $visitor['latitude'],
                $visitor['longitude'],
                $visitor['accuracy'],
                $visitor['photo_filename'] ?: '无',
                $visitor['user_agent'],
                $visitor['referer']
            ]);
        }
        fclose($output);
    } else {
        header('Content-Type: application/json; charset=utf-8');
        header('Content-Disposition: attachment; filename="' . $filename . '.json"');
        echo json_encode($visitors, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    }
}

// 读取跳转配置
$redirectConfig = [
    'redirect_url' => '',
    'redirect_delay' => 3,
    'enable_redirect' => 0
];

if (file_exists('redirect_config.json')) {
    $configData = json_decode(file_get_contents('redirect_config.json'), true);
    if ($configData) {
        $redirectConfig = array_merge($redirectConfig, $configData);
    }
}

// 读取访客数据
try {
    $db = new Database();
    $visitors = $db->getAllVisitors();
    $stats = $db->getVisitorStats();
} catch (Exception $e) {
    $visitors = [];
    $stats = ['total' => 0, 'last_24h' => 0, 'last_7d' => 0, 'today' => 0];
    $error_message = $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>访客数据管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .header {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            color: #333;
            margin: 0;
        }
        .logout {
            background: #dc3545;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        .logout:hover {
            background: #c82333;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        .stat-card h3 {
            color: #666;
            font-size: 14px;
            margin-bottom: 10px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .stat-card .number {
            font-size: 36px;
            font-weight: bold;
            color: #333;
        }
        .visitor-card {
            background: white;
            margin-bottom: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .visitor-header {
            background: #f8f9fa;
            padding: 15px 20px;
            border-bottom: 1px solid #dee2e6;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .visitor-content {
            padding: 20px;
        }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        .info-section h4 {
            color: #495057;
            margin-bottom: 10px;
            padding-bottom: 5px;
            border-bottom: 2px solid #e9ecef;
        }
        .info-item {
            margin-bottom: 8px;
            display: flex;
            align-items: center;
        }
        .info-item strong {
            min-width: 80px;
            color: #6c757d;
        }
        .photo-container {
            text-align: center;
            margin-top: 20px;
        }
        .photo-container img {
            max-width: 300px;
            max-height: 300px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            transition: transform 0.3s;
        }
        .photo-container img:hover {
            transform: scale(1.05);
        }
        .no-data {
            text-align: center;
            padding: 50px;
            color: #6c757d;
        }
        .management-panel {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin: 5px;
            font-size: 14px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-warning {
            background-color: #ffc107;
            color: #212529;
        }
        .btn-warning:hover {
            background-color: #e0a800;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        .form-inline {
            display: inline-block;
            margin: 5px;
        }
        .form-inline input, .form-inline select {
            padding: 6px;
            border: 1px solid #ddd;
            border-radius: 3px;
            margin-right: 5px;
        }
        .message {
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .visitor-actions {
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid #dee2e6;
        }
        .checkbox-column {
            width: 40px;
            text-align: center;
        }
        .select-all-container {
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>访客数据管理系统</h1>
        <a href="?logout=1" class="logout">退出登录</a>
        <a href="db_admin.php" class="btn" style="float: right; margin-right: 10px;">数据库管理</a>
        <div style="clear: both;"></div>
    </div>

    <?php if ($message): ?>
        <div class="message <?php echo $messageType; ?>">
            <?php echo htmlspecialchars($message); ?>
        </div>
    <?php endif; ?>

    <div class="management-panel">
        <h3>🛠️ 数据管理操作</h3>
        
        <div style="display: flex; flex-wrap: wrap; gap: 10px; align-items: center;">
            <!-- 批量删除 -->
            <form method="post" id="batchDeleteForm" style="display: inline;">
                <input type="hidden" name="action" value="batch_delete">
                <button type="button" class="btn btn-danger" onclick="batchDelete()">批量删除选中</button>
            </form>
            
            <!-- 清理旧数据 -->
            <form method="post" class="form-inline" onsubmit="return confirm('确定要清理旧数据吗？此操作不可恢复！')">
                <input type="hidden" name="action" value="cleanup_old">
                <input type="number" name="cleanup_days" value="30" min="1" max="365" placeholder="天数" style="width: 80px;">
                <button type="submit" class="btn btn-warning">清理旧数据</button>
            </form>
            
            <!-- 数据导出 -->
            <form method="post" class="form-inline">
                <input type="hidden" name="action" value="export_data">
                <select name="export_format">
                    <option value="csv">CSV格式</option>
                    <option value="json">JSON格式</option>
                </select>
                <button type="submit" class="btn btn-success">导出数据</button>
            </form>
            
            <!-- 其他链接 -->
            <a href="index.php" class="btn">访客页面</a>
            <a href="test.php" class="btn">系统测试</a>
        </div>
    </div>

    <!-- 跳转链接管理 -->
    <div class="management-panel">
        <h3>🔗 跳转链接管理</h3>
        <p style="color: #666; margin-bottom: 15px;">配置访客数据提交成功后的跳转链接</p>
        
        <form method="post" style="max-width: 600px;">
            <input type="hidden" name="action" value="update_redirect_url">
            
            <div style="margin-bottom: 15px;">
                <label style="display: block; margin-bottom: 5px; font-weight: bold;">
                    <input type="checkbox" name="enable_redirect" value="1" 
                           <?php echo $redirectConfig['enable_redirect'] ? 'checked' : ''; ?> 
                           style="margin-right: 8px;">
                    启用自动跳转
                </label>
            </div>
            
            <div style="margin-bottom: 15px;">
                <label style="display: block; margin-bottom: 5px; font-weight: bold;">跳转链接:</label>
                <input type="url" name="redirect_url" 
                       value="<?php echo htmlspecialchars($redirectConfig['redirect_url']); ?>" 
                       placeholder="https://example.com" 
                       style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                <small style="color: #666;">留空则不跳转，请输入完整的URL（包含http://或https://）</small>
            </div>
            
            <div style="margin-bottom: 15px;">
                <label style="display: block; margin-bottom: 5px; font-weight: bold;">跳转延迟时间:</label>
                <select name="redirect_delay" style="padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                    <option value="1" <?php echo $redirectConfig['redirect_delay'] == 1 ? 'selected' : ''; ?>>1秒</option>
                    <option value="2" <?php echo $redirectConfig['redirect_delay'] == 2 ? 'selected' : ''; ?>>2秒</option>
                    <option value="3" <?php echo $redirectConfig['redirect_delay'] == 3 ? 'selected' : ''; ?>>3秒</option>
                    <option value="5" <?php echo $redirectConfig['redirect_delay'] == 5 ? 'selected' : ''; ?>>5秒</option>
                    <option value="10" <?php echo $redirectConfig['redirect_delay'] == 10 ? 'selected' : ''; ?>>10秒</option>
                </select>
                <small style="color: #666; margin-left: 10px;">数据提交成功后等待多长时间开始跳转</small>
            </div>
            
            <button type="submit" class="btn btn-success">💾 保存配置</button>
            
            <div style="margin-top: 15px; padding: 10px; background: #f8f9fa; border-radius: 5px; border-left: 4px solid #17a2b8;">
                <strong>当前配置状态:</strong><br>
                <span style="color: <?php echo $redirectConfig['enable_redirect'] ? '#28a745' : '#dc3545'; ?>;">
                    <?php echo $redirectConfig['enable_redirect'] ? '✅ 已启用' : '❌ 已禁用'; ?>
                </span>
                <?php if ($redirectConfig['enable_redirect'] && $redirectConfig['redirect_url']): ?>
                    <br>跳转到: <a href="<?php echo htmlspecialchars($redirectConfig['redirect_url']); ?>" target="_blank"><?php echo htmlspecialchars($redirectConfig['redirect_url']); ?></a>
                    <br>延迟: <?php echo $redirectConfig['redirect_delay']; ?>秒
                <?php endif; ?>
            </div>
        </form>
    </div>

    <!-- 统计信息 -->
    <div class="stats-grid">
        <div class="stat-card">
            <h3>📊 总访客数</h3>
            <div class="number"><?php echo $stats['total']; ?></div>
        </div>
        <div class="stat-card">
            <h3>🕐 今日访客</h3>
            <div class="number"><?php echo $stats['today']; ?></div>
        </div>
        <div class="stat-card">
            <h3>📅 24小时内</h3>
            <div class="number"><?php echo $stats['last_24h']; ?></div>
        </div>
        <div class="stat-card">
            <h3>📈 7天内</h3>
            <div class="number"><?php echo $stats['last_7d']; ?></div>
        </div>
    </div>

    <?php if (isset($error_message)): ?>
        <div class="visitor-card">
            <div class="visitor-content">
                <div class="no-data">
                    <h3>⚠️ 数据库连接错误</h3>
                    <p><?php echo htmlspecialchars($error_message); ?></p>
                    <p><a href="setup_database.php">点击这里设置数据库</a></p>
                </div>
            </div>
        </div>
    <?php elseif (empty($visitors)): ?>
        <div class="visitor-card">
            <div class="visitor-content">
                <div class="no-data">
                    <h3>📭 暂无访客数据</h3>
                    <p>还没有访客访问过您的页面</p>
                    <p><a href="index.php">点击这里访问访客页面</a></p>
                </div>
            </div>
        </div>
    <?php else: ?>
        <div class="select-all-container">
            <label>
                <input type="checkbox" id="selectAll" onchange="toggleSelectAll()"> 
                <strong>全选/取消全选</strong>
            </label>
            <span style="margin-left: 20px; color: #666;">
                共 <?php echo count($visitors); ?> 条记录
            </span>
        </div>
        
        <?php foreach ($visitors as $visitor): ?>
            <div class="visitor-card">
                <div class="visitor-header">
                    <div style="display: flex; align-items: center;">
                        <input type="checkbox" name="visitor_select" value="<?php echo htmlspecialchars($visitor['id']); ?>" 
                               style="margin-right: 10px;" onchange="updateSelectAll()">
                        <strong>访客 ID: <?php echo htmlspecialchars(substr($visitor['id'], -8)); ?>...</strong>
                    </div>
                    <div>
                        <?php echo htmlspecialchars($visitor['timestamp']); ?>
                    </div>
                </div>
                <div class="visitor-content">
                    <div class="info-grid">
                        <div class="info-section">
                            <h4>🌍 位置信息</h4>
                            <div class="info-item">
                                <strong>纬度:</strong> <?php echo htmlspecialchars($visitor['latitude']); ?>
                            </div>
                            <div class="info-item">
                                <strong>经度:</strong> <?php echo htmlspecialchars($visitor['longitude']); ?>
                            </div>
                            <div class="info-item">
                                <strong>精度:</strong> <?php echo htmlspecialchars($visitor['accuracy']); ?> 米
                            </div>
                            <div class="info-item">
                                <strong>地图:</strong> 
                                <a href="https://uri.amap.com/marker?position=<?php echo $visitor['longitude']; ?>,<?php echo $visitor['latitude']; ?>&name=访客位置&src=访客追踪系统" 
                                   target="_blank" style="color: #007bff;">📍 在高德地图中查看</a>
                            </div>
                        </div>
                        
                        <div class="info-section">
                            <h4>💻 设备信息</h4>
                            <div class="info-item">
                                <strong>IP:</strong> <?php echo htmlspecialchars($visitor['ip_address']); ?>
                            </div>
                            <div class="info-item">
                                <strong>设备:</strong> <?php echo htmlspecialchars(substr($visitor['user_agent'], 0, 50)); ?>...
                            </div>
                            <div class="info-item">
                                <strong>来源:</strong> <?php echo htmlspecialchars($visitor['referer'] ?: '直接访问'); ?>
                            </div>
                            <div class="info-item">
                                <strong>时间:</strong> <?php echo htmlspecialchars($visitor['client_timestamp']); ?>
                            </div>
                        </div>
                    </div>

                    <?php if (!empty($visitor['photo_filename'])): ?>
                        <div class="photo-container">
                            <h4>📷 访客照片</h4>
                            <img src="visitor_data/photos/<?php echo htmlspecialchars($visitor['photo_filename']); ?>" 
                                 alt="访客照片" 
                                 onclick="window.open(this.src, '_blank')" 
                                 style="cursor: pointer;" />
                            <p><small>点击图片查看大图</small></p>
                        </div>
                    <?php endif; ?>
                    
                    <div class="visitor-actions">
                        <h4>🔧 操作</h4>
                        <form method="post" style="display: inline;" 
                              onsubmit="return confirm('确定要删除这条访客记录吗？此操作不可恢复！')">
                            <input type="hidden" name="action" value="delete_visitor">
                            <input type="hidden" name="visitor_id" value="<?php echo htmlspecialchars($visitor['id']); ?>">
                            <button type="submit" class="btn btn-danger">🗑️ 删除记录</button>
                        </form>
                        
                        <?php if (!empty($visitor['photo_filename'])): ?>
                            <a href="visitor_data/photos/<?php echo htmlspecialchars($visitor['photo_filename']); ?>" 
                               download class="btn">📥 下载照片</a>
                        <?php endif; ?>
                        
                        <button type="button" class="btn" onclick="copyToClipboard('<?php echo htmlspecialchars($visitor['id']); ?>')">
                            📋 复制ID
                        </button>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <script>
        // 全选/取消全选功能
        function toggleSelectAll() {
            const selectAll = document.getElementById('selectAll');
            const checkboxes = document.querySelectorAll('input[name="visitor_select"]');
            
            checkboxes.forEach(checkbox => {
                checkbox.checked = selectAll.checked;
            });
        }
        
        // 更新全选状态
        function updateSelectAll() {
            const checkboxes = document.querySelectorAll('input[name="visitor_select"]');
            const selectAll = document.getElementById('selectAll');
            const checkedCount = document.querySelectorAll('input[name="visitor_select"]:checked').length;
            
            selectAll.checked = checkedCount === checkboxes.length;
            selectAll.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
        }
        
        // 批量删除功能
        function batchDelete() {
            const checkedBoxes = document.querySelectorAll('input[name="visitor_select"]:checked');
            
            if (checkedBoxes.length === 0) {
                alert('请先选择要删除的记录');
                return;
            }
            
            if (!confirm(`确定要删除选中的 ${checkedBoxes.length} 条记录吗？此操作不可恢复！`)) {
                return;
            }
            
            const form = document.getElementById('batchDeleteForm');
            
            // 清除之前的隐藏字段
            const existingInputs = form.querySelectorAll('input[name="selected_visitors[]"]');
            existingInputs.forEach(input => input.remove());
            
            // 添加选中的访客ID
            checkedBoxes.forEach(checkbox => {
                const input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'selected_visitors[]';
                input.value = checkbox.value;
                form.appendChild(input);
            });
            
            form.submit();
        }
        
        // 复制到剪贴板功能
        function copyToClipboard(text) {
            if (navigator.clipboard) {
                navigator.clipboard.writeText(text).then(() => {
                    alert('ID已复制到剪贴板');
                }).catch(() => {
                    fallbackCopyTextToClipboard(text);
                });
            } else {
                fallbackCopyTextToClipboard(text);
            }
        }
        
        function fallbackCopyTextToClipboard(text) {
            const textArea = document.createElement("textarea");
            textArea.value = text;
            document.body.appendChild(textArea);
            textArea.focus();
            textArea.select();
            
            try {
                document.execCommand('copy');
                alert('ID已复制到剪贴板');
            } catch (err) {
                alert('复制失败，请手动复制: ' + text);
            }
            
            document.body.removeChild(textArea);
        }
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            updateSelectAll();
        });
    </script>
</body>
</html>